/**
 * @file ngclient.cpp
 * @author Suk Robert Koh (srkoh87z@gmail.com)
 */

#include "ngine/client/ngclient.h"

/**
 * Default constructor for NGClient class
 */
NGClient::NGClient()
{
	// initialize variables
	recvQueue = new NGMessageQueue();
	sendQueue = new NGMessageQueue();

	// allocate space
	recvBuffer = (char *) malloc(DEFAULT_RECV_BUF_SIZE);
	bufferPosition = 0;

	// use default message coder
	messageCoder = new NGDefaultMessageCoder();

	NGLogger::debug("NGClient constructor finished");
}

/**
 * Destructor for NGClient class
 */
NGClient::~NGClient()
{
	// release recvBuffer
	if(recvBuffer)
		free(recvBuffer);
}

/**
 * Sends message to server
 *
 * @param	NGMessage *		Pointer to the message to be sent to server
 * @return	int				Error code
 *
 * TODO: make use of int return
 */
int NGClient::sendToServer(NGMessage *msg)
{
	sendQueue->push(msg);

	NGLogger::info("pushed msg into sendQueue");

	/**
	 * TODO: implement error checking/handling
	 */
	return 0;
}

/**
 * Sets/replaces the message coder to be used
 *
 * @param	NGMessageCoder *	Pointer to the message coder to be used
 */
void NGClient::setMessageCoder(NGMessageCoder *_messageCoder)
{
	// release if messageCoder already points to one
	if(messageCoder)
		free(messageCoder);

	messageCoder = _messageCoder;
}

/**
 * Gets the receive message queue size
 *
 * @return	int		Size of the receive message queue
 */
int NGClient::getRecvQueueSize(void)
{
	return recvQueue->size();
}

/**
 * Gets the first message of the receive message queue
 *
 * @return	NGMessage *		Pointer to the first message of the receive message queue
 */
NGMessage *NGClient::getFirstMessage(void)
{
	if(recvQueue->size() > 0)
		return recvQueue->pop();
}
